package UI;

/*
 * 这里展示好友列表
 * 好友从数据库调用
 * 按钮数量根据好友数量决定
 * 点击好友的用户名进入聊天界面
 * */

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class FriendListUI extends JFrame implements MouseListener {
    // 好友集合，用于存储好友的用户名
    private List<String> friendList = new ArrayList<>();


    BufferedReader br = null;


    //返回主界面按钮
    JButton returnHome = new JButton("返回主页");
    //添加好友按钮
    JButton addFriend = new JButton("添加好友");

    public FriendListUI() {
        WholeUI wholeUI = new WholeUI(this) {
            @Override
            protected void elseWindow(JFrame jFrame) {
                jFrame.setSize(500, 500);
                jFrame.setTitle("好友列表");
            }
        };
        //加载好友列表
        loadFriendList();
        //加载按钮
        loadUI();


    }
    //io流 (可乐盒子)
    //从本地FriendList/当前登录的用户名.txt中读取好友列表
    //当前登录的用户名通过这个调用 -> LoginUI.userNameText.getText()
    // 生成一个好友集合比如 List<String> friendList = new ArrayList<>();

    //根据好友生成按钮,
    // 根据好友用户名生成按钮文本
    //并把按钮放入一个集合ButtonList中,泛型为<JButton>

//        for (String name : friendList) {
//        JButton button = new JButton(name);
    //        ButtonList.add(button);
//        }
//
    public void loadFriendList() {
        try {
            // 从本地读取好友列表
            br = new BufferedReader(new FileReader("documents/FriendList/" + LoginUI.userNameText.getText() + ".txt"));
            String line;
            while ((line = br.readLine()) != null) {
                friendList.add(line);
            }

        } catch (IOException e) {
            //如果没有文件就创建文件
            try {
                File file = new File("documents/FriendList/" + LoginUI.userNameText.getText() + ".txt");
                file.createNewFile();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    //这里是界面的布局
    private void loadUI() {

        // 创建一个面板并设置布局管理器
        JPanel panel = new JPanel(new GridLayout(0, 5)); // 5列
        getContentPane().add(panel);

        int size = friendList.size();
        int padding = 10;
        int buttonWidth = 100;
        int buttonHeight = 60;
        int cols = 5; // 每行显示5个按钮

        for (int i = 0; i < size; i++) {
            String name = friendList.get(i);
            System.out.println("读取到好友:" + name);
            JButton button = new JButton(name);
            button.setBounds(padding + (buttonWidth + padding) * (i % cols),
                    padding + (buttonHeight + padding) * (i / cols),
                    buttonWidth, buttonHeight);
            this.getContentPane().add(button);
            button.addMouseListener(this);


        }


        // 返回主页按钮
        returnHome.setBounds(0, 300, 100, 50);
        this.getContentPane().add(returnHome);
        returnHome.addMouseListener(this);

        // 添加好友按钮
        addFriend.setBounds(0, 360, 100, 50);
        this.getContentPane().add(addFriend);
        addFriend.addMouseListener(this);
    }






    @Override
    public void mouseClicked(MouseEvent e) {
        //点击好友按钮进入聊天界面
        // (只要不是返回主界面按钮或添加好友被点击了,就进入聊天界面)
        //这里逻辑已经写完了不用再写
        JButton clickedButton = (JButton) e.getSource();
        if (clickedButton!=returnHome &&clickedButton!=addFriend){
            ChatUI chatUI = null;
            try {
                chatUI = new ChatUI(clickedButton.getText());
            } catch (SocketException ex) {
                throw new RuntimeException(ex);
            } catch (UnknownHostException ex) {
                throw new RuntimeException(ex);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            this.dispose();

            //返回主界面
        } else if (clickedButton==returnHome) {
            HomeUI homeUI = new HomeUI();
            this.dispose();

            //进入添加好友界面
        } else if (clickedButton==addFriend) {
            AddFriendUI addFriendUI = new AddFriendUI();
            this.dispose();
        }

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}